package org.web.webdisplay.server.controller;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.web.base.domain.ResultDO;
import org.web.base.domain.ViewResultHelper;
import org.web.base.domain.helper.ServiceExceptionHelper;
import org.web.base.domain.helper.ValidationHelper;
import org.web.base.domain.view.ViewResultDTO;

import jakarta.annotation.Resource;
import java.util.List;
import java.util.ArrayList;

import org.web.webdisplay.sdk.dto.query.QueryDisplayInfoRequest;
import org.web.webdisplay.sdk.dto.DisplayInfoDTO;
import org.web.webdisplay.server.biz.impl.UserPageQueryInfoBizImpl;
import org.web.webdisplay.sdk.biz.DisplayInfoBiz;
import org.web.webdisplay.server.service.UserPageQueryInfoService;

@Controller("displayInfo")
@RequestMapping("displayInfo")
public class DisplayInfoController {

    private static final Logger logger = LogManager.getLogger(DisplayInfoController.class);

    @Resource
    private DisplayInfoBiz displayInfoBiz;
    @Resource
    private UserPageQueryInfoService userPageQueryInfoService;
    @Resource
    private UserPageQueryInfoBizImpl userPageQueryInfoBiz;


    @RequestMapping("selectDisplayInfoList")
    @ResponseBody
    public ViewResultDTO<List<DisplayInfoDTO>> selectDisplayInfoList(@RequestBody QueryDisplayInfoRequest queryDisplayInfoRequest) {
        ViewResultDTO<List<DisplayInfoDTO>> viewResultDO = new ViewResultDTO<>(true);
        try {
            // 查询对象校验, 如果调用BIZ层方法，则无需校验，因为BIZ自会进行校验。
            ValidationHelper.validateWithThrow(QueryDisplayInfoRequest.class, queryDisplayInfoRequest);
            ResultDO<List<DisplayInfoDTO>> selectResult = displayInfoBiz.selectDisplayInfoList(queryDisplayInfoRequest);
            viewResultDO.setData(selectResult.getDomain());
        } catch (Exception e) {
            logger.error(ServiceExceptionHelper.getExceptionInfo(e));
            viewResultDO = ViewResultHelper.buildViewResultByServiceException(e, null);
        }
        return viewResultDO;
    }



    @RequestMapping("saveDisplayInfo")
    @ResponseBody
    public ViewResultDTO<DisplayInfoDTO> saveDisplayInfo(@RequestBody DisplayInfoDTO displayInfoDTO) {
        try {
            List<DisplayInfoDTO> list = new ArrayList<>();
            list.add(displayInfoDTO);
            ResultDO<List<DisplayInfoDTO>> batchResultDO = displayInfoBiz.batchSaveDisplayInfoList(list);
            ResultDO<DisplayInfoDTO> resultDO = new ResultDO<>();
            if (batchResultDO.isSuccess()) {
                resultDO.setDomain(batchResultDO.getDomain().get(0));
                return ViewResultHelper.convertToViewResultByResultDO(resultDO, DisplayInfoDTO.class);
            } else {
                return ViewResultHelper.convertToViewResultByResultDO(batchResultDO, DisplayInfoDTO.class);
            }
        } catch (Exception e) {
            logger.error(ServiceExceptionHelper.getExceptionInfo(e));
            return ViewResultHelper.buildViewResultByServiceException(e, null);
        }
    }

    @RequestMapping("batchDeleteDisplayInfoList")
    @ResponseBody
    public ViewResultDTO<List<Integer>> batchDeleteDisplayInfoList(@RequestBody List<Integer> ids) {
        ViewResultDTO<List<Integer>> viewResultDTO = new ViewResultDTO<>(false);
        try {
            viewResultDTO.setData(ids);
            List<DisplayInfoDTO> list = new ArrayList<>();
            for (Integer id : ids) {
                DisplayInfoDTO displayInfoDTO = new DisplayInfoDTO();
                displayInfoDTO.setDisplayInfoId(id);
                displayInfoDTO.setIsDelete("Y");
                list.add(displayInfoDTO);
            }
            displayInfoBiz.batchSaveDisplayInfoList(list);
            viewResultDTO.setResult(true);
            viewResultDTO.setMsg("批量删除成功");
        } catch (Exception e) {
            logger.error(ServiceExceptionHelper.getExceptionInfo(e));
            return ViewResultHelper.buildViewResultByServiceException(e,null);
        }
        return viewResultDTO;
    }

}
